package com.lx.pc.restful.user;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;
import com.lx.pc.domain.vo.user.UserVo;
import com.lx.pc.restful.BaseController;
import com.lx.pc.restful.usercookie.UserCookie;
import com.lx.pc.restful.util.AjaxResult;
import com.lx.pc.service.user.IUserService;
import com.lx.pc.utils.Constants;
import com.lx.pc.utils.Utils;

@Controller
public class UserController extends BaseController{
	
	@Resource(name = "userService")
	private IUserService userService;
	
//	@Resource(name = "springRedisTemplate")
//	private SpringRedisTemplate springRedisTemplate;
	
	/**
	 * 跳转登陆页面
	 * @return
	 */
	@RequestMapping("/toLogin.do")
	public ModelAndView toLogin() {
		ModelAndView mv = new ModelAndView("/login");
		
		return mv;
	}
	
	/**
	 * 跳转注册
	 * @return
	 */
	@RequestMapping("/toRegister.do")
	public ModelAndView toRegister() {
		ModelAndView mv = new ModelAndView("/register");
		return mv;
	}
	
	/**
	 * 跳转找回密码
	 * @return
	 */
	@RequestMapping("/toFindpwd.do")
	public ModelAndView toFindpwd() {
		ModelAndView mv = new ModelAndView("/findPwd");
		return mv;
	}
	
	/**
	 * 查询名是否存在
	 * @param v
	 * @param page
	 * @param size
	 * @return
	 * @throws IOException
	 */
	@RequestMapping("/serchUserName")
	@ResponseBody
    public  String serchUserName(UserVo v,Integer page,Integer size) throws IOException {
		
        Map<String,Object> result = new HashMap<String, Object>();
        
        UserVo userVo = userService.findByUsername(v);
        if (userVo != null) {
        	result.put("result", "1");
        }else {
        	result.put("result", "0");
        }
		
        return JSON.toJSONString(result);
    }
	
	/**
	 * 登陆
	 * @param v
	 * @param page
	 * @param size
	 * @return
	 * @throws IOException
	 * @throws ServletException 
	 * chkLogin 是否记住密码
	 */
	@RequestMapping("/login.do")
	@ResponseBody
    public AjaxResult<UserVo> login(UserVo v,HttpServletRequest request,HttpServletResponse response,String chkLogin) {
		AjaxResult<UserVo> ajaxResult = new AjaxResult<UserVo>();

		UserVo userVo = userService.findByUsername(v);
		
        if (userVo != null) {
        	String password = Utils.md5(v.getPassword(), Constants.PWD_CACHE_KEY);
        	if (userVo.getPassword().equals(password)) {
        		
				if (chkLogin != null) {
					// 保存用户登录Cookie
					UserCookie userCookie = new UserCookie();
					userCookie.addUserInfoCookie(request,response, userVo);
				}
				ajaxResult.setMsg("1");
        	}else {
        		ajaxResult.setMsg("2");
        	}
        }else {
        	ajaxResult.setMsg("3");
        }
        return ajaxResult;
    }
	
	
	/**
	 * 注册
	 * @param v
	 * @return
	 * @throws IOException
	 */
	@RequestMapping("/register.do")
	@ResponseBody
    public Map<String,Object> register(UserVo v,HttpServletRequest request, HttpServletResponse response) throws IOException {
		
        Map<String,Object> result = new HashMap<String, Object>();
        
        if (v != null) {
        	v.setPassword(Utils.md5(v.getPassword(), Constants.PWD_CACHE_KEY));
        	userService.saveUser(v);
        	
        	UserCookie userCookie = new UserCookie();
        	v.setbId(v.getId().toString());
			userCookie.addUserInfoCookie(request,response, v);
        	
        }else {
        	result.put("userVo", 0);
        }
        
		result.put("id", v.getId());
        return result;
    }
	
	/**
	 * 退出登录
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/logout.do", method = RequestMethod.GET)
	public String logout(HttpServletRequest request, HttpServletResponse response){
		//点击退出登录时清除用户登录cookie
		UserCookie.removeCookie(response);
		request.getSession().invalidate();
		// 将用户请求转发到首页
		return "redirect:/index.do";
	}
}
	


